package lj.courseinterect.entity;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

@javax.persistence.Entity
public class Teacher implements Entity, UserDetails
{
    public enum TeaState {
        NORMAL, // 正常
        CLOSED, // 已关闭
        OTHERS // 其他
    }

    /**
     * 主键id
     */
	@Id
	@GeneratedValue
    @Column(name = "tea_id")
	private Long teaId;

    /**
     * 学号
     */
    @Column(name = "tea_num", updatable = false, unique = true, nullable = false)
    private String teaNum;

    /**
     * 专业id
     */
    @Column(name = "major_id", nullable = false)
    private int majorId;

    /**
     * 姓名
     */
    @Column(name = "tea_name", nullable = false)
    private String teaName;

    /**
     * 密码
     */
	@Column(name = "tea_pwd", length = 80, nullable = false)
	private String teaPwd;

    /**
     * 权限集
     */
	@ElementCollection(fetch = FetchType.EAGER)
	private Set<String> roles = new HashSet<String>();

    /**
     * 学生状态
     */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "tea_state", columnDefinition = "int(11) default 0")
    private TeaState teaState;

	protected Teacher()
	{
		/* Reflection instantiation */
	}


	public Teacher(String stuNum, String stuPwdHash)
	{
		this.teaNum = stuNum;
		this.teaPwd = stuPwdHash;
	}

    public Long getTeaId() {
        return teaId;
    }

    public void setTeaId(Long teaId) {
        this.teaId = teaId;
    }

    public String getTeaNum() {
        return teaNum;
    }

    public void setTeaNum(String teaNum) {
        this.teaNum = teaNum;
    }

    public int getMajorId() {
        return majorId;
    }

    public void setMajorId(int majorId) {
        this.majorId = majorId;
    }

    public String getTeaName() {
        return teaName;
    }

    public void setTeaName(String teaName) {
        this.teaName = teaName;
    }

    public String getTeaPwd() {
        return teaPwd;
    }

    public void setTeaPwd(String teaPwd) {
        this.teaPwd = teaPwd;
    }

    public Set<String> getRoles() {
        return roles;
    }

    public void setRoles(Set<String> roles) {
        this.roles = roles;
    }

    public TeaState getTeaState() {
        return teaState;
    }

    public void setTeaState(TeaState teaState) {
        this.teaState = teaState;
    }

    public void addRole(String role)
    {
        this.roles.add(role);
    }

    @Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		Set<String> roles = this.getRoles();

		if (roles == null) {
			return Collections.emptyList();
		}

		Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
		for (String role : roles) {
			authorities.add(new SimpleGrantedAuthority(role));
		}

		return authorities;
	}

    @Override
    public String getPassword() {
        return this.teaPwd;
    }

    @Override
    public String getUsername() {
        return this.teaNum;
    }


	@Override
	public boolean isAccountNonExpired()
	{
		return true;
	}


	@Override
	public boolean isAccountNonLocked()
	{
		return true;
	}


	@Override
	public boolean isCredentialsNonExpired()
	{
		return true;
	}


	@Override
	public boolean isEnabled()
	{
		return true;
	}

}